********************************************************************************
* APPENDIX TABLE A2: LAB-IN-THE-FIELD EXPERIMENTS	 						   *
********************************************************************************
use "$temporales\Bcom_976total_2022.dta", clear
	merge m:1 codmerc using "$temporales\Bmdo_90total_2022.dta", nogen 

* Generate standardized version of the contribution to the games:
	su p_aporte2
	gen std_contri_public=(p_aporte2-r(mean))/r(sd) //Public goods game
	su p_aporte3
	gen std_contri_trust=(p_aporte3-r(mean))/r(sd) //Trust games
	su p_devolucion
	gen std_devol_trust=(p_devolucion-r(mean))/r(sd) //Trust game's trustworthiness

	foreach game in contri_trust devol_trust contri_public {
		eststo: areg std_`game' $ties, absorb(zona20) $vce
		estadd local zone_fe "YES"
		estadd local market_ctls "NO"
		estadd local vendor_ctls "NO"
		
		eststo: areg std_`game' $ties $mdo, absorb(zona20) $vce
		estadd local zone_fe "YES"
		estadd local market_ctls "YES"
		estadd local vendor_ctls "NO"
		
		eststo: areg std_`game' $ties $mdo $ven, absorb(zona20) $vce
		estadd local zone_fe "YES"
		estadd local market_ctls "YES"
		estadd local vendor_ctls "YES"
	}
	
	* Export:
	esttab using "${tablas}/tablaA2_games.tex", replace booktabs label b(%9.3f) se(%9.3f) nocon ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(N r2 zone_fe market_ctls vendor_ctls, fmt(%5.2f) labels(Observations "R-squared" "Zone fixed effects" "Market controls" "Vendor controls")) ///
		mgroups( "\textit{Contribution trust game}" "\textit{Trustworthiness}" "\textit{Contribution public goods game}", pattern(1 0 0 1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}) ) ///
		nomtitle keep($ties)
		
	eststo clear
	
*************************************************************************************************
* APPENDIX TABLE A4: ENFORCEMENT AND INFRASTRUCTURE GROWTH WITH ENFORCEMENT AT THE VENDOR LEVEL *
*************************************************************************************************
* Generate dummies indicating sanctions perceptions (by fraction)
	gen enf_form1=inlist(indenf_nopaga, 4,5,6,7)
	gen enf_form2=inlist(indenf_estafa,4,5,6,7)
	gen enf_form3=inlist(indenf_limpia,4,5,6,7)

* Compute share of fractions where vendor expects sanctions
egen enf_form_ind=rmean(enf_form1 enf_form2 enf_form3)
	la var enf_form_ind "Formal Enforcement"

* OLS:
	eststo: xi:  ivreg2 $bp $ties $bp07 i.zona20, $vce

	eststo: xi:  ivreg2 $bp $bp07 enf_form_ind i.zona20, $vce

	eststo: xi:  ivreg2 $bp $bp07 enf_form_ind $mdo i.zona20, $vce

	eststo: xi:  ivreg2 $bp $bp07 enf_form_ind $mdo $ven i.zona20, $vce

* Three-panel table set-up:
	local preheadA  "\begin{tabular}{l*{4}{c}} \hline"
	local postheadA "\midrule \multicolumn{5}{l}{\textbf{Panel A: OLS – Public Goods and Formal Enforcement}} \\  \multicolumn{5}{l}{\textit{Dep. var.: Change in Market Infrastructure (Market Investment) Index, 2007–2014}} \\ "
	local postheadB "\midrule \multicolumn{5}{l}{\textbf{Panel B: IV First Stage – Formal Enforcement and Social Ties}} \\  \multicolumn{5}{l}{\textit{Dep. var.: Formal Enforcement}} \\ "
	local postheadC "\midrule \multicolumn{5}{l}{\textbf{Panel C: IV Second Stage – Public Goods and Formal Enforcement}} \\  \multicolumn{5}{l}{\textit{Dep. var.: Change in Market Infrastructure (Market Investment) Index, 2007–2014}} \\ "
	local postfootC "\bottomrule \end{tabular}"

* Export OLS results:
	esttab using "${tablas}/tablaA4_vendor.tex", replace fragment booktabs  prehead(`preheadA') posthead(`postheadA') label b(%9.3f) se(%9.3f) nocon ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(N r2, fmt(%5.2f) labels(Observations "R-squared")) ///
		nomtitle keep($ties enf_form_ind )
	

* IV - First stage:
	eststo: xi: ivreg2 $bp (enf_form_ind=$ties) $bp07 i.zona20, $vce first savefirst savefprefix(tab4_1stnoc)
	estimates restore tab4_1stnoc*
	eststo tab4_1stnoc
	xi:  ivreg2 $bp (enf_form_ind=$ties) $bp07 i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': tab4_1stnoc // Save Kleibergen-Papp F statistic
	reg enf_form_ind $ties $bp07 i.zona20, $vce
	estadd scalar r2_f=`e(r2)': tab4_1stnoc //Save First stage R2

	eststo: xi:  ivreg2 $bp (enf_form_ind=$ties) $bp07 $mdo i.zona20, $vce first savefirst savefprefix(tab4_1stmdoc)
	estimates restore tab4_1stmdoc*
	eststo tab4_1stmdoc
	xi:  ivreg2 $bp (enf_form_ind=$ties) $bp07 $mdo i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': tab4_1stmdoc // Save Kleibergen-Papp F statistic
	reg enf_form_ind $ties $bp07 $mdo i.zona20, $vce
	estadd scalar r2_f=`e(r2)': tab4_1stmdoc //Save First stage R2

	eststo: xi:  ivreg2 $bp (enf_form_ind=$ties) $bp07 $mdo $ven i.zona20,$vce first savefirst savefprefix(tab4_1stvenc)
	estimates restore tab4_1stvenc*
	eststo tab4_1stvenc
	xi:  ivreg2 $bp (enf_form_ind=$ties) $bp07 $mdo $ven i.zona20,$vce
	estadd scalar KP_F=`e(rkf)': tab4_1stvenc // Save Kleibergen-Papp F statistic
	reg enf_form_ind $ties $bp07 $mdo $ven i.zona20,$vce
	estadd scalar r2_f=`e(r2)': tab4_1stvenc //Save First stage R2

	* Export
	esttab  tab4_1stnoc tab4_1stmdoc tab4_1stvenc using "${tablas}/tablaA4_vendor.tex", append fragment label b(%9.3f) se(%9.3f) nocon booktabs ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(KP_F N r2_f, fmt(%5.2f) labels( "Kleibergen-Papp F statistic" Observations "R-squared")) nonumber ///
		nomtitle keep($ties) extracols(1) posthead(`postheadB') 
	eststo clear 

* IV - Second stage:
	eststo iv_nocon : xi:  ivregress 2sls $bp (enf_form_ind=$ties) $bp07 i.zona20, $vce
	estat weakrobust, ci
	estadd local zone_fe "YES"
	estadd local market_ctls "NO"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_nocon
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_nocon

	eststo iv_mdo : xi:  ivregress 2sls $bp (enf_form_ind=$ties) $bp07 $mdo i.zona20, $vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_mdo 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_mdo 

	eststo iv_ven : xi: ivregress 2sls $bp (enf_form_ind=$ties) $bp07 $mdo $ven i.zona20,$vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "YES"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_ven 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_ven

	* Export only main variables:
	esttab iv_nocon iv_mdo iv_ven using "${tablas}/tablaA4_vendor.tex", append fragment label b(%9.3f) se(%9.3f) nocon booktabs ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(upper lower N zone_fe market_ctls vendor_ctls , fmt(%5.3g) labels( " Upper A-R 95\% CI" " Lower A-R 95\% CI" Observations "Zone fixed effects" "Market controls" "Vendor controls")) nonumber ///
		nomtitle keep(enf_form_ind) extracols(1) posthead(`postheadC') postfoot(`postfootC')

	eststo clear
	estimates clear
	

*****************************************************************************************
* APPENDIX TABLE A3: ENFORCEMENT AND INFRASTRUCTURE GROWTH WITH DATASET AT MARKET LEVEL *
*****************************************************************************************
use "$temporales\Bmdo_90total_2022.dta", clear

* Define label of variables:
	la var $ties "Social ties among market founders (1-0)"
	la var $enf "Formal Enforcement"

	la var enf_form "Formal Enforcement" 
	la var puestos "Number of stalls in market"
	la var antiguedad "Age of market"
	la var p_funda "Percent of market stalls owned by founders (CENAMA)"
	la var p_verdura "Percent of stalls that sell vegetables (CENAMA)"
	la var p_fruta "Percent of stalls that sell fruit (CENAMA)"
	la var p_carne "Percent of stalls that sell meat (CENAMA)"
	la var p_ave "Percent of stalls that sell chicken (CENAMA)"
	la var p_pes "Percent of stalls that sell fish (CENAMA)"
	la var p_abar "Percent of stalls that sell dried goods (CENAMA)"
	la var indmdos17 "Index of number/distance of traditional markets around market (2017)"
	la var indmdos07 "Index of number/distance of traditional markets around market (2007)"
	la var indsuper "Index of number/distance of supermarkets around market"
	la var Medad "Average age of respondent vendors"
	la var Mfundador "Percent of respondent vendors that are market founders"
	la var Mnac_fuera "Perfent of respondent vendors born outside Lima"
	la var Medu_nivel  "Average education of respondent vendors (years)"
	la var Bpcomplex_2007 "Market infrastructure stock 2007"
	la var tienece "Market currently collecting extraordinary dues"

* OLS:
	eststo: xi:  ivreg2 $bp $ties $bp07 i.zona20, $vce

	eststo: xi:  ivreg2 $bp $bp07 $enf i.zona20, $vce

	eststo: xi:  ivreg2 $bp $bp07 $enf $mdo i.zona20, $vce

	eststo: xi:  ivreg2 $bp $bp07 $enf $mdo $ven2 i.zona20, $vce

* Three-panel table set-up
	local preheadA  "\begin{tabular}{l*{4}{c}} \hline" 
	local postheadA "\midrule \multicolumn{5}{l}{\textbf{Panel A: OLS – Public Goods and Formal Enforcement}} \\  \multicolumn{5}{l}{\textit{Dep. var.: Change in Market Infrastructure (Market Investment) Index, 2007–2014}} \\ "
	local postheadB "\midrule \multicolumn{5}{l}{\textbf{Panel B: IV First Stage – Formal Enforcement and Social Ties}} \\  \multicolumn{5}{l}{\textit{Dep. var.: Formal Enforcement}} \\ "
	local postheadC "\midrule \multicolumn{5}{l}{\textbf{Panel C: IV Second Stage – Public Goods and Formal Enforcement}} \\  \multicolumn{5}{l}{\textit{Dep. var.: Change in Market Infrastructure (Market Investment) Index, 2007–2014}} \\ "
	local postfootC "\bottomrule \end{tabular}" 

* Export OLS results:
	esttab using "${tablas}/tablaA3_market.tex", replace fragment booktabs  prehead(`preheadA') posthead(`postheadA') label b(%9.3f) se(%9.3f) nocon ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(N r2, fmt(%5.2f) labels(Observations "R-squared")) ///
		nomtitle keep($ties $enf )
		

* IV - First stage:
	eststo: xi: ivreg2 $bp ($enf=$ties) $bp07 i.zona20, $vce first savefirst savefprefix(tab4_1stnoc)
	estimates restore tab4_1stnoc*
	eststo tab4_1stnoc
	xi:  ivreg2 $bp ($enf=$ties) $bp07 i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': tab4_1stnoc // Save Kleibergen-Papp F statistic
	reg $enf $ties $bp07 i.zona20, $vce
	estadd scalar r2_f=`e(r2)': tab4_1stnoc //Save First stage R2

	eststo: xi:  ivreg2 $bp ($enf=$ties) $bp07 $mdo i.zona20, $vce first savefirst savefprefix(tab4_1stmdoc)
	estimates restore tab4_1stmdoc*
	eststo tab4_1stmdoc
	xi:  ivreg2 $bp ($enf=$ties) $bp07 $mdo i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': tab4_1stmdoc // Save Kleibergen-Papp F statistic
	reg $enf $ties $bp07 $mdo i.zona20, $vce
	estadd scalar r2_f=`e(r2)': tab4_1stmdoc //Save First stage R2

	eststo: xi:  ivreg2 $bp ($enf=$ties) $bp07 $mdo $ven2 i.zona20,$vce first savefirst savefprefix(tab4_1stvenc)
	estimates restore tab4_1stvenc*
	eststo tab4_1stvenc
	xi:  ivreg2 $bp ($enf=$ties) $bp07 $mdo $ven2 i.zona20,$vce
	estadd scalar KP_F=`e(rkf)': tab4_1stvenc // Save Kleibergen-Papp F statistic
	reg $enf $ties $bp07 $mdo $ven2 i.zona20,$vce
	estadd scalar r2_f=`e(r2)': tab4_1stvenc //Save First stage R2

	* Export:
	esttab  tab4_1stnoc tab4_1stmdoc tab4_1stvenc using "${tablas}/tablaA3_market.tex", append fragment label b(%9.3f) se(%9.3f) nocon booktabs ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(KP_F N r2_f, fmt(%5.2f) labels( "Kleibergen-Papp F statistic" Observations "R-squared")) nonumber ///
		nomtitle keep($ties) extracols(1) posthead(`postheadB') 
	eststo clear 

* IV - Second stage:
	eststo iv_nocon : xi:  ivregress 2sls $bp ($enf=$ties) $bp07 i.zona20, $vce
	estat weakrobust, ci
	estadd local zone_fe "YES"
	estadd local market_ctls "NO"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_nocon
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_nocon

	eststo iv_mdo : xi:  ivregress 2sls $bp ($enf=$ties) $bp07 $mdo i.zona20, $vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_mdo 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_mdo 

	eststo iv_ven : xi: ivregress 2sls $bp ($enf=$ties) $bp07 $mdo $ven2 i.zona20,$vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "YES"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_ven 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_ven

* Export only main variables:
	esttab iv_nocon iv_mdo iv_ven using "${tablas}/tablaA3_market.tex", append fragment label b(%9.3f) se(%9.3f) nocon booktabs ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(upper lower N zone_fe market_ctls vendor_ctls , fmt(%5.3g) labels( " Upper A-R 95\% CI" " Lower A-R 95\% CI" Observations "Zone fixed effects" "Market controls" "Vendor controls")) nonumber ///
		nomtitle keep($enf) extracols(1) posthead(`postheadC') postfoot(`postfootC')

	eststo clear
	
* End of 3. appendix.do